package com.redoop.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 日期类
 */
public class DateUtil {

    public static final String YMDHMS = "yyyy-MM-dd HH:mm:ss";
    public static final String YMDHMS_CN = "yyyy年MM月dd日 HH点mm分ss秒";
    public static final String MDHM = "MM-dd HH:mm";
    public static final String YM = "yyyy-MM";
    public static final String YMD = "yyyy-MM-dd";
    public static final String HMS = "HH:mm:ss";

    /**
     * 指定日期转long
     *
     * @param time
     * @return
     * @throws ParseException
     */
    public static long _getTime(String time, String _format) {// 单位毫秒
        SimpleDateFormat format = new SimpleDateFormat(_format);
        try {
            return format.parse(time).getTime();
        } catch (ParseException e) {
            return 0;
        }
    }

    /**
     * 获取当前毫秒级时间
     */
    public static long getTime() {// 单位毫秒
        return new Date().getTime();
    }

    /**
     * 获取当前不同格式的日期
     *
     * @param format
     * @return
     */
    public static String getTime(String format) {
        SimpleDateFormat Format = new SimpleDateFormat(format);
        return Format.format(new Date());
    }

    public static String convertTime(long time, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat("", Locale.CHINESE);
        sdf.applyPattern(format);
        return sdf.format(time);
    }

    public static Date toTime(String time, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat("", Locale.CHINESE);
        sdf.applyPattern(format);
        try {
            return sdf.parse(time);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 秒转换毫秒
     *
     * @return
     */
    public static long SEC_MSEL(Object sec) {
        Float sec_f = 0f;
        try {
            sec_f = Float.parseFloat(sec.toString());
        } catch (Exception e) {
        }
        return (long) (sec_f * 1000);
    }

    /**
     * 分转换毫秒
     *
     * @param min
     * @return
     */
    public static long MIN_MSEL(Object min) {
        Float min_f = 0f;
        try {
            min_f = Float.parseFloat(min.toString()) * 60;
        } catch (Exception e) {
        }
        return SEC_MSEL(min_f);
    }

    /**
     * 小时换毫秒
     *
     * @param
     * @return
     */
    public static long HR_MSEL(Object hr) {
        Float hr_f = 0f;
        try {
            hr_f = Float.parseFloat(hr.toString()) * 60;
        } catch (Exception e) {
        }
        return MIN_MSEL(hr_f);
    }

    /**
     * 天转换毫秒
     *
     * @param
     * @return
     */
    public static long DAY_MSEL(Object day) {
        Float day_f = 0f;
        try {
            day_f = Float.parseFloat(day.toString()) * 24;
        } catch (Exception e) {
        }
        return HR_MSEL(day_f);
    }

    public static String vs(String t1, String t2) {
        long d1 = DateUtil._getTime(t1, DateUtil.YMDHMS);
        long d2 = DateUtil._getTime(t2, DateUtil.YMDHMS);
        return d1 > d2 ? t2 : t1;
    }
    
    /**
     * 时间戳转日期
     */
    public static String timeStampToDate(String time, String format){
    	SimpleDateFormat sdf = new SimpleDateFormat(format);
    	return sdf.format(new Date(Long.valueOf(time + "000")));
    }
    
    /**
     * utc的秒时间转换成本地时间字符串
     * @param utcLong
     * @param format
     * @return
     */
    public static String utclongToLocal(long utcLong,String format){
    	utcLong=utcLong+8*3600;
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		String dateStr = sdf.format(new Date(utcLong*1000));
		return dateStr;
		
	}
    
    
    /**
     * 通过本地年月日时间获取对应utc（秒）当天的开始时间
     * @param local  本地年月日时间
     * @return utc当天开始时间
     * @throws ParseException
     */
    public static long localToUtcStartTime(String local,String format) throws ParseException{
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Date d = sdf.parse(local);
		long start = Date.UTC(d.getYear(), d.getMonth(), d.getDate(), 0, 0, 0) / 1000 - (8 * 3600);
		return start;
	}
    
    /**
     * 通过本地年月日时间获取对应utc（秒）当天的开始时间
     * @param local  本地年月日时间
     * @return utc当天结束时间
     * @throws ParseException
     */
    public static long localToUtcEndTime(String local,String format) throws ParseException{
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Date d = sdf.parse(local);
		long end = Date.UTC(d.getYear(), d.getMonth(), d.getDate()+1, 0, 0, 0) / 1000 - (8 * 3600);
		return end;
	}
    
    
    public static long getDayEndTime(){
    	Date date = new Date();
    	Calendar cal = Calendar.getInstance();
    	cal.setTime(date);
    	cal.set(Calendar.HOUR_OF_DAY, 23);
    	cal.set(Calendar.MINUTE, 59);
    	cal.set(Calendar.SECOND, 59);
    	return cal.getTimeInMillis();
    }
    
    public static Date getMondayOfThisWeek(){
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        return cal.getTime();
    }
    
    public static Date getNowDate(){
        Calendar cal = Calendar.getInstance();
        return cal.getTime();
    }
    
    public static String getYMDHmsForNowTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("", Locale.CHINESE);
        sdf.applyPattern("yyyyMMddHHmmss");
        return sdf.format(new Date());
    }
    
    /**
     * 
     * @param date
     * @return
     */
    public static int getDaysOfMonth(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }
    
    /**
     * yyyy-MM
     * @param date
     * @return
     */
    public static int getDaysOfMonth(String date){
        Date time = toTime(date, YM);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }
    
    /**
     * 
     * @param beginDate 2021-03-02
     * @param endDate 2021-09-01
     * @return [2021-03, 2021-04, 2021-05, 2021-06, 2021-07, 2021-08, 2021-09]
     */
    public static List<String> getBetweenMonth(String beginDate, String endDate){
        List<String> list = new ArrayList<String>();
        Calendar begin = Calendar.getInstance();
        Calendar end = Calendar.getInstance();
        begin.setTime(toTime(beginDate, YM));
        begin.set(begin.get(Calendar.YEAR), begin.get(Calendar.MONTH), 1);
        end.setTime(toTime(endDate, YM));
        end.set(end.get(Calendar.YEAR), end.get(Calendar.MONTH), 2);
        Calendar curr = begin;
        while(curr.before(end)){
            list.add(convertTime(curr.getTimeInMillis(), YM));
            curr.add(Calendar.MONTH, 1);
        }
        return list;
    }
    
    /**
     * yyyy-MM
     * @param date 2021-05
     * @return 2021-05-01 00:00:00
     */
    public static long getBeginTimeOfMonth(String date){
        Date time = toTime(date, YM);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTimeInMillis();
    }
    
    /**
     * yyyy-MM
     * @param date 2021-05
     * @return 2021-05-01 00:00:00
     */
    public static long getEndTimeOfMonth(String date){
        Date time = toTime(date, YM);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DATE));
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTimeInMillis();
    }
}